﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace YourWallet
{
    public class XMLManagement
    {
        string fileName = "data.xml";

        public XMLManagement()
        {
        }

        public void WriteXML(List<Transaction> trans)
        {
            using (XmlWriter writer = XmlWriter.Create(fileName))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Transactions");

                foreach (Transaction transaction in trans)
                {
                    writer.WriteStartElement("Transaction");

                    writer.WriteElementString("AccountName", transaction.GetAccountName());
                    writer.WriteElementString("Amount", transaction.GetAmount().ToString());
                    writer.WriteElementString("Category", transaction.GetCategory());
                    writer.WriteElementString("Date", transaction.GetDate().ToString());
                    writer.WriteElementString("Note", transaction.GetNote());
                    writer.WriteElementString("TransactionType", transaction.GetTransactionType().ToString());

                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }

        public List<Transaction> ReadXML()
        {
            List<Transaction> trans = new List<Transaction>();
            if (File.Exists(fileName))
            {
                XmlDocument xmlDoc = new XmlDocument();
                try
                {
                    xmlDoc.Load(fileName);
                    int count = xmlDoc.GetElementsByTagName("Transaction").Count;

                    for (int i = 0; i < count; i++)
                    {
                        string accountName = xmlDoc.GetElementsByTagName("AccountName").Item(i).InnerText;
                        double amount = Double.Parse(xmlDoc.GetElementsByTagName("Amount").Item(i).InnerText);
                        string category = xmlDoc.GetElementsByTagName("Category").Item(i).InnerText;
                        DateTime date = Convert.ToDateTime(xmlDoc.GetElementsByTagName("Date").Item(i).InnerText);
                        string note = xmlDoc.GetElementsByTagName("Note").Item(i).InnerText;  
                        TransactionType type = TransactionType.Income;
                        if (xmlDoc.GetElementsByTagName("TransactionType").Item(i).InnerText == "Expense")
                        {
                            type = TransactionType.Expense;
                        }
                        trans.Add(new Transaction(accountName, amount, category, date, note, type));
                    }
                }
                catch (XmlException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            return trans;
        }

    }                   
                            
}
